package com.bsoft.service.impl;

import com.bsoft.Exception.CommonException;
import com.bsoft.aop.annotation.MultiDataSourceTransactional;
import com.bsoft.mapper.business.HosptalOrderBillMapper;
import com.bsoft.service.HosptalOrderBillService;
import com.bsoft.utils.ConmonUtil;
import com.bsoft.utils.DateUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import java.util.*;

@Service
@EnableTransactionManagement
public class HosptalOrderBillServiceImpl extends ConmonUtil implements HosptalOrderBillService {
    private static Logger logger = LoggerFactory.getLogger(HosptalOrderBillServiceImpl.class);

    @Autowired
    HosptalOrderBillMapper hosOrderBillMapper;


    /*
     * 门诊 取消/执行 检查状态更新
     */
    @Override
    @MultiDataSourceTransactional(transactionManagers = {"hisTransactionManager","portalTransactionManager"})
    public String InspectionStatusUpdate(Map<String, Object> req) throws Exception {
        //获取病人姓名
        String brxm = ((Map<String, Object>) req.get("Patient")).get("Name").toString();
        String type = ((Map<String, Object>) req.get("Patient")).get("SourcePatientIdType").toString();
        //获取申请单号
        String sqdh = ((Map<String, Object>) req.get("ExamStatusInfo")).get("RequestId").toString();
        String czgh = ((Map<String, Object>) req.get("ExamStatusInfo")).get("OperatingNumber").toString();
        String czrq = ((Map<String, Object>) req.get("ExamStatusInfo")).get("OperatingDateTime").toString();

        if (!ObjectUtils.isEmpty(czgh))
            DateUtil.stringToDatePt(((Map<String, Object>) req.get("ExamStatusInfo")), "OperatingDateTime", "yyyyMMddThhmmss");

        //根据申请单和姓名查询申请单
        req.put("brxm", brxm);
        req.put("sqdh", sqdh);
        List<Map<String, Object>> sqdList = hosOrderBillMapper.getSqd(req);
        if (CollectionUtils.isEmpty(sqdList))
            throw new CommonException("根据申请单号[" + sqdh + "]和姓名[" + brxm + "]未查到申请单[emr_jcsq]信息!");

        //循环查询项目状态
        String examStatusCode = "";
        String jczt = "";
        int n = 0;
        boolean update = true;

        List<Map<String, Object>> xmList = new ArrayList<>();
        if ((((Map<String, Object>) ((Map<String, Object>) req.get("ExamStatusInfo")).get("ItemList")).get("Item")) instanceof List) {
            xmList = (List<Map<String, Object>>) ((Map<String, Object>) ((Map<String, Object>) req.get("ExamStatusInfo")).get("ItemList")).get("Item");
        } else {
            xmList.add((Map<String, Object>) ((Map<String, Object>) ((Map<String, Object>) req.get("ExamStatusInfo")).get("ItemList")).get("Item"));
        }

        /*OV:门诊档案 IV:住院档案 HV:体检档案 -> 5:检查登记/7:检查执行 --当为住院档案 并且 项目检查状态为 5或者为7 就不能进入*/
        if (!("IV".equals(type) && ("5".equals(examStatusCode) || "7".equals(examStatusCode)))) {

            for (Map<String, Object> xm : xmList) {
                //判断所有项目是不是同一状态
                if (!examStatusCode.equals("") && !examStatusCode.equals(xm.get("examStatusCode").toString())) {
                    update = false;
                }
                examStatusCode = xm.get("examStatusCode").toString();
                String zlxmid = xm.get("ItemCode").toString();
                if ("7".equals(examStatusCode) || "8".equals(examStatusCode) || "5".equals(examStatusCode)) {
                    jczt = "3";
                } else if ("3".equals(examStatusCode) || "11".equals(examStatusCode)) {
                    jczt = "2";
                } else if ("9".equals(examStatusCode)) {
                    jczt = "4";
                } else if ("10".equals(examStatusCode)) {
                    jczt = "5";
                } else if ("12".equals(examStatusCode) || "14".equals(examStatusCode) || "6".equals(examStatusCode) || "4".equals(examStatusCode)) {
                    jczt = "1";
                    n = hosOrderBillMapper.delectJcbg(req);
                    if (n < 1 || n > 500) logger.info("根据申请单号" + sqdh + ",未找到报告，无法删除");
                } else {
                    throw new CommonException("现在只支持examStatusCode7,9,10,12状态更新");
                }
                //更新EMR_JCXM
                req.put("zlxmid", zlxmid);
                req.put("jczt", jczt);
                req.put("OrderId", xm.get("OrderId"));
                n = hosOrderBillMapper.updateEMR_JCXM(req);
                if (n < 1 || n > 500) {
                    throw new CommonException("根据申请单号" + sqdh + ",未找到子项目" + zlxmid);
                }

                List<Map<String, Object>> bqyzList = null;
                xm.put("sqdh", sqdh);

                if ("OV".equals(type)) {//门诊
                    outpatientAccountingReport(bqyzList, xm, examStatusCode, czgh, req);
                }

            }

            //如果所有明细申请单同一个状态，则更新申请单
            if (update && !jczt.equals("")) {
                hosOrderBillMapper.updateEMR_JCSQ(req);
            }
            return thePlatformReturnedSuccessfully("HIS", "ODS_03070009_SUC", "3.3");
        }

        return inpatientRegistrationExecution("HIS", "ODS_03070009_SUC", "3.3");
    }

    /*
     * SVR_ODS_2204-检查计费确认通知
     *  计费 和 状态更新一起的
     */
    @Override
    @MultiDataSourceTransactional(transactionManagers = {"hisTransactionManager","portalTransactionManager"})
    public String ExmRequestPayBill(Map<String, Object> req) throws Exception {

        //经济核算
        List<Map<String, Object>> jjhsList = new ArrayList<>();
        String sqdh = req.get("RequestId") + "";
        //根据申请单查询申请单
        req.put("sqdh", sqdh);
        List<Map<String, Object>> sqdList = hosOrderBillMapper.getSqd(req);

        if (CollectionUtils.isEmpty(sqdList))
            throw new CommonException("根据申请单[" + sqdh + "]未查到表EMR_JCSQ的申请单!");

        Long zyh = Long.valueOf(sqdList.get(0).get("JZHM").toString());
        //是否出院
        List<Map<String, Object>> zyInfoList = hosOrderBillMapper.zyInfo(zyh);
        if (CollectionUtils.isEmpty(zyInfoList))
            throw new CommonException("该患者[zyh:" + zyh + "]已经出院(CYPB!=0),不能在计费!");

        req.put("brxm", sqdList.get(0).get("BRXM"));

        List<Map<String, Object>> xmList = new ArrayList<>();
        if (req.get("Item") instanceof List) {
            xmList = (List<Map<String, Object>>) req.get("Item");
        } else {
            xmList.add((Map<String, Object>) req.get("Item"));
        }

        int n = 0;
        boolean update = true;

        for (Map<String, Object> xm : xmList) {
            isNUll(xm, Arrays.asList("Operator", "OperateDateTime", "OrderId", "ItemCode"));
            String OrderId = xm.get("OrderId").toString();
            String zlxmid = xm.get("ItemCode").toString();
            xm.put("OrderId", OrderId);
            xm.put("qrgh", xm.get("Operator"));
            xm.put("ysdm", xm.get("Operator"));
            xm.put("sqdh", sqdh);

            DateUtil.stringToDatePt(xm, "OperateDateTime", "yyyyMMddTHHmmss");

            //更新zy_bqyz
            xm.put("ysyzbh", xm.get("OrderId"));
            List<Map<String, Object>> bqyzInfolist = hosOrderBillMapper.selectBqyz(xm);
            if (CollectionUtils.isEmpty(bqyzInfolist))
                throw new CommonException("请联系护士站对医嘱进行复核,申请单号为::" + sqdh + "::医嘱本号为::" + OrderId);

            if ("1".equals(bqyzInfolist.get(0).get("LSBZ").toString()))
                throw new CommonException("该医嘱已经计费请勿重复计费,申请单号为::" + sqdh + "::医嘱本号为::" + OrderId);

            n = hosOrderBillMapper.updateBqyz(xm);
            if (n < 1 || n > 500)
                throw new CommonException("根据检查项目代码OrderId未查到有效的申请单明细,ysyzbh" + xm.get("OrderId"));

            //验证医生是否存在
            List<Map<String, Object>> ysxxList = hosOrderBillMapper.getYsxx(xm);
            if (CollectionUtils.isEmpty(ysxxList)) throw new CommonException("该医生代码医生不存在");

            xm.put("jlxh", getIdentity("zy", "ZY_FYMX", n));
            xm.put("n", n);

            //插入zy_fymx
            hosOrderBillMapper.saveFymx(xm);
            //更新 BQ_YZJH
            n = hosOrderBillMapper.updateYZJH(xm);
            if (n < 1 || n > 500)
                throw new CommonException("根据检查项目代码OrderId查到有效的医嘱计划,是否医嘱计划未提交或者医嘱已经执行,ysyzbh为" + xm.get("ysyzbh"));

            //更新YJ_ZY01
            n = hosOrderBillMapper.updateZY01(xm);
            if (n < 1 || n > 500) throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY01，是否医技未提交");

            //更新YJ_ZY02
            n = hosOrderBillMapper.updateZY02(xm);
            if (n < 1 || n > 500) throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY02，是否医技未提交");

            req.put("zlxmid", zlxmid);
            req.put("jczt", "3");
            req.put("OrderId", xm.get("OrderId"));
            n = hosOrderBillMapper.updateEMR_JCXM(req);
            if (n < 1 || n > 500) throw new CommonException("根据申请单号" + sqdh + ",未找到子项目" + zlxmid);

            Map<String, Object> jzxh = hosOrderBillMapper.getJZXH(xm);

            List<Map<String, Object>> bqyzList = hosOrderBillMapper.getBQYZ(jzxh);

            for (Map<String, Object> map : bqyzList) {
                Long jlxh = getIdentity("yj", "YJ_BG01_JJHS");
                map.put("JLXH", jlxh);
                map.put("JCYS", xm.get("ysdm"));
                map.put("MZZY", "2");
                map.put("JCRQ", xm.get("OperateDateTime"));
                try {
                    int a = hosOrderBillMapper.insertJJHS(map);
                    if (a == 0) {
                        logger.info("经济核算表插入失败");
                    }
                } catch (Exception e) {
                    logger.info("经济核算表插入失败");
                }
            }

            //先判断物资库存 判断是否需要二级库房出库
            Map<String, Object> wl_xhmx = new HashMap<>();
            List<Map<String, Object>> wz_list = hosOrderBillMapper.wz1(xm);
            req.put("csmc", "WZXHMX_WDZKF_HCJF");
            String WZXHMX_WDZKF_HCJF = hosOrderBillMapper.getXtcs(xtcsMap("WZXHMX_WDZKF_HCJF"));
            if (WZXHMX_WDZKF_HCJF == null || WZXHMX_WDZKF_HCJF.equals("")) {
                throw new CommonException("'机构未设置WZXHMX_WDZKF_HCJF对应数据来源的系统参数");
            }

            if (wz_list.size() > 0) {
                for (Map<String, Object> wz_map : wz_list) {
                    String fymxJlxh = wz_map.get("JLXH").toString();
                    //查询科室物资库房对照
                    List<Map<String, Object>> ks_list = hosOrderBillMapper.wz6(wz_map);
                    if ("1".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        continue;
                    } else if ("0".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        throw new CommonException("ksdm" + wz_map.get("KSDM") + "未对应物资库房");
                    }
                    //查询物资库存是否满足
                    List<Map<String, Object>> wzkc_list = hosOrderBillMapper.wz2(wz_map);
                    if (wzkc_list.size() < 1) {
                        throw new CommonException("物资库存不足");
                    }
                    wz_map.putAll(wzkc_list.get(0));

                    n = hosOrderBillMapper.wz3(wz_map);
                    if (n != 1) {
                        throw new CommonException("物资库存不足,更新物资库存失败");
                    }
                    //写wl_xhmx
                    //10获取记录序号
                    Long jlxh = getIdentity("wl", "WL_XHMX");
                    wl_xhmx.put("jlxh", jlxh);
                    wl_xhmx.put("jgid", "1");
                    wl_xhmx.put("kfxh", wz_map.get("KFXH"));
                    wl_xhmx.put("ksdm", wz_map.get("KSDM"));
                    wl_xhmx.put("ksmc", wz_map.get("KSMC"));
                    wl_xhmx.put("brid", wz_map.get("BRID"));
                    wl_xhmx.put("brhm", wz_map.get("MZHM"));
                    wl_xhmx.put("brly", "2");
                    wl_xhmx.put("brxm", wz_map.get("BRXM"));
                    // wl_xhmx.put("xhrq",);
                    wl_xhmx.put("wzxh", wz_map.get("WZXH"));
                    wl_xhmx.put("wzsl", wz_map.get("YLSL"));
                    wl_xhmx.put("wzjg", wz_map.get("WZJG"));
                    wl_xhmx.put("thsl", 0);
                    wl_xhmx.put("ztbz", 0);
                    wl_xhmx.put("djxh", 0);
                    wl_xhmx.put("sbxh", fymxJlxh);
                    wl_xhmx.put("kcxh", wz_map.get("KCXH"));
                    wl_xhmx.put("cjxh", wz_map.get("CJXH"));
                    wl_xhmx.put("yjxh", wz_map.get("SBXH"));
                    n = hosOrderBillMapper.wz5(wl_xhmx);
                }
            }
        }
        hosOrderBillMapper.updateEMR_JCSQ1(req);
        return "<BSXml><MsgHeader><Sender>HIS</Sender><Status>true</Status><ErrCode></ErrCode><Detail>SVR_ODS_2204-检查计费确认通知成功</Detail></MsgHeader></BSXml>";
    }


    /*
     * SVR_ODS_2205-取消检查计费通知
     */
    @Override
    @MultiDataSourceTransactional(transactionManagers = {"hisTransactionManager","portalTransactionManager"})
    public String ExmRequestReturnBill(Map<String, Object> req) throws Exception {

        //获取申请单号
        String sqdh = req.get("RequestId") + "";
        //根据申请单查询申请单
        req.put("sqdh", sqdh);

        List<Map<String, Object>> sqdList = hosOrderBillMapper.getSqd(req);
        if (CollectionUtils.isEmpty(sqdList)) throw new CommonException("根据申请单号:" + sqdh + "未查到申请单");

        req.put("brxm", sqdList.get(0).get("BRXM"));
        String zyh = sqdList.get(0).get("JZHM") + "";

        //是否出院
        List<Map<String, Object>> zyInfoList = hosOrderBillMapper.zyInfo(Long.valueOf(zyh));
        if (CollectionUtils.isEmpty(zyInfoList))
            throw new CommonException("该患者已经出院(CYPB!=0),不能在取消计费!!zyh=" + zyh);

        List<Map<String, Object>> xmList = new ArrayList<>();
        if (req.get("Item") instanceof List) {
            xmList = (List<Map<String, Object>>) req.get("Item");
        } else {
            xmList.add((Map<String, Object>) req.get("Item"));
        }

        int n = 0;
        for (Map<String, Object> xm : xmList) {
            xm.put("zyh", zyh);

            isNUll(xm, Arrays.asList("Operator", "OperateDateTime", "OrderId", "ItemCode"));
            String OrderId = xm.get("OrderId").toString();
            String zlxmid = xm.get("ItemCode").toString();

            xm.put("OrderId", OrderId);
            xm.put("qrgh", xm.get("Operator"));
            xm.put("ysdm", xm.get("Operator"));
            xm.put("ysyzbh", OrderId);
            DateUtil.stringToDatePt(xm, "OperateDateTime", "yyyyMMddThhmmss");
            //更新zy_bqyz
            n = hosOrderBillMapper.updateBqyzZf(xm);
            if (n < 1 || n > 500) throw new CommonException("根据检查项目代码OrderId未查到有效的申请单明细");

            //验证操作工,验证医生是否存在
            List<Map<String, Object>> ysxxList = hosOrderBillMapper.getYsxx(xm);
            if (ysxxList.size() < 1) {
                throw new CommonException("该医生代码医生不存在");
            }

            xm.put("jlxh", getIdentity("zy", "ZY_FYMX", n));
            xm.put("n", n);
            //判断是否需要二级库房出库
            Map<String, Object> wl_xhmx = new HashMap<>();

            List<Map<String, Object>> wz_list = hosOrderBillMapper.wz1Tf(xm);
            req.put("csmc", "WZXHMX_WDZKF_HCJF");
            String WZXHMX_WDZKF_HCJF = hosOrderBillMapper.getXtcs(xtcsMap("WZXHMX_WDZKF_HCJF"));
            if (WZXHMX_WDZKF_HCJF == null || WZXHMX_WDZKF_HCJF.equals(""))
                throw new CommonException("'机构未设置WZXHMX_WDZKF_HCJF对应数据来源的系统参数");

            if (wz_list.size() > 0) {
                for (Map<String, Object> wz_map : wz_list) {
                    String fymxJlxh = wz_map.get("JLXH").toString();
                    //查询科室物资库房对照
                    List<Map<String, Object>> ks_list = hosOrderBillMapper.wz6(wz_map);
                    if ("1".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        continue;
                    } else if ("0".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        throw new CommonException("ksdm" + wz_map.get("KSDM") + "未对应物资库房");
                    }
                    //查询物资库存是否满足
                    List<Map<String, Object>> wzkc_list = hosOrderBillMapper.wz2Tf(wz_map);
                    if (wzkc_list.size() < 1) {
                        throw new CommonException("WZXH" + wz_map.get("WZXH") + "预扣数量小于退费数量，更新物资库存失败");
                    }
                    wz_map.putAll(wzkc_list.get(0));

                    n = hosOrderBillMapper.wz3Tf(wz_map);
                    if (n != 1)
                        throw new CommonException("WZXH" + wz_map.get("WZXH") + "预扣数量小于退费数量,更新物资库存失败");
                    //写wl_xhmx,获取记录序号
                    Long jlxh = getIdentity("wl", "WL_XHMX");
                    wl_xhmx.put("jlxh", jlxh);
                    wl_xhmx.put("jgid", "1");
                    wl_xhmx.put("kfxh", wz_map.get("KFXH"));
                    wl_xhmx.put("ksdm", wz_map.get("KSDM"));
                    wl_xhmx.put("ksmc", wz_map.get("KSMC"));
                    wl_xhmx.put("brid", wz_map.get("BRID"));
                    wl_xhmx.put("brhm", wz_map.get("MZHM"));
                    wl_xhmx.put("brly", "2");
                    wl_xhmx.put("brxm", wz_map.get("BRXM"));
                    wl_xhmx.put("wzxh", wz_map.get("WZXH"));
                    wl_xhmx.put("wzsl", wz_map.get("TFSL"));
                    wl_xhmx.put("wzjg", wz_map.get("WZJG"));
                    wl_xhmx.put("thsl", 0);
                    wl_xhmx.put("ztbz", 0);
                    wl_xhmx.put("djxh", 0);
                    wl_xhmx.put("sbxh", fymxJlxh);
                    wl_xhmx.put("kcxh", wz_map.get("KCXH"));
                    wl_xhmx.put("cjxh", wz_map.get("CJXH"));
                    wl_xhmx.put("yjxh", wz_map.get("SBXH"));
                    n = hosOrderBillMapper.wz5(wl_xhmx);
                }
            }
            //更新zy_fymx YTSL
            n = hosOrderBillMapper.updateFymx(xm);
            if (n < 1 || n > 500) throw new CommonException("根据检查项目代码OrderId查到费用明细");
            //查询zy_fymx
            List<Map<String, Object>> fymxList = hosOrderBillMapper.getFymxZf(xm);
            for (Map<String, Object> fymx : fymxList) {
                //插入zy_fymx
                hosOrderBillMapper.saveFymxZf(fymx);
            }
            //更新 BQ_YZJH
            n = hosOrderBillMapper.updateYZJHZf(xm);
            if (n < 1 || n > 500) {
                throw new CommonException("根据检查项目代码OrderId查到有效的医嘱计划，是否医嘱计划未执行");
            }
            //更新YJ_ZY01
            n = hosOrderBillMapper.updateZY01Zf(xm);
            if (n < 1 || n > 500) {
                throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY01,是否医嘱未执行");
            }
            //更新YJ_ZY02
            n = hosOrderBillMapper.updateZY02Zf(xm);
            if (n < 1 || n > 500) {
                throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY02,是否医嘱未执行");
            }
            req.put("zlxmid", zlxmid);
            req.put("JCZT", "1");
            req.put("OrderId", xm.get("OrderId"));
            n = hosOrderBillMapper.updateEMR_JCXM(req);
            if (n < 1 || n > 500) {
                throw new CommonException("根据申请单号" + sqdh + ",未找到子项目" + zlxmid);
            }


            Map<String, Object> jzxh = hosOrderBillMapper.getJZXH(xm);

            List<Map<String, Object>> bqyzList = hosOrderBillMapper.getQXBQYZ(jzxh);

            for (Map<String, Object> map : bqyzList) {
                Long jlxh = getIdentity("yj", "YJ_BG01_JJHS");
                map.put("JLXH", jlxh);
                map.put("JCYS", xm.get("ysdm"));
                map.put("MZZY", "2");
                map.put("JCRQ", xm.get("OperateDateTime"));
                try {
                    int a = hosOrderBillMapper.insertJJHS(map);
                    if (a == 0) {
                        logger.info("取消经济核算表插入失败");
                    }
                } catch (Exception e) {
                    logger.info("取消经济核算表插入失败");
                }
            }
        }

        hosOrderBillMapper.updateEMR_JCSQ1(req);

        return "<BSXml><MsgHeader><Sender>HIS</Sender><Status>true</Status><ErrCode></ErrCode><Detail>SVR_ODS_2205-取消检查计费通知</Detail></MsgHeader></BSXml>";
    }

    /*
     * 非检查医嘱 计费(住院 无申请点ID 如:药品,治疗等)
     */
    @Override
    @MultiDataSourceTransactional(transactionManagers = {"hisTransactionManager","portalTransactionManager"})
    public String noExamineOrderBill(Map<String, Object> msgBody) throws Exception {
        Map<String, Object> examStatusInfo = (Map<String, Object>) msgBody.get("ExamStatusInfo");
        //判断入参不能为空
        isNUll(examStatusInfo, Arrays.asList("zyh", "ItemList"));
        //判断是否存在网络导致 二次调用接口
        List<Map<String, Object>> xmList = new ArrayList<>();
        if ((((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item")) instanceof List) {
            xmList = (List<Map<String, Object>>) ((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item");
        } else {
            xmList.add((Map<String, Object>) ((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item"));
        }
        for (Map<String, Object> req : xmList) {
            int count = 0;
            req.put("zyh", examStatusInfo.get("zyh"));
            req.put("jhh", req.get("jhh"));
            req.put("czgh", req.get("executionCode"));
            //获取操作工号
            List<Map<String, Object>> ysxxList = hosOrderBillMapper.getNosqIDYSXX(req);
            if (CollectionUtils.isEmpty(ysxxList)) throw new CommonException("该医生代码医生不存在");

            //1.zy_bqyz更新
            List<Map<String, Object>> bqyzList = hosOrderBillMapper.getNoSQIDBQYZ(req);
            if (CollectionUtils.isEmpty(bqyzList))
                throw new CommonException("未找到相关医嘱信息!!::" + req.get("ysyzbh"));

            for (Map<String, Object> bqyz : bqyzList) {
                if ("1".equals(bqyz.get("LSYZ").toString()) && "1".equals(bqyz.get("LSBZ").toString())) {
                    throw new CommonException("该住院号::" + bqyz.get("ZYH") + "有相关医嘱已经执行请核实重新执行!!::" + req.get("ysyzbh"));
                }
            }
            count = hosOrderBillMapper.updateNOSQIDBqyz(req);
            if (count == 0) throw new CommonException("zy_bqyz更新失败");

            //获取主键
            Long identity = getIdentity("zy", "ZY_FYMX", count);
            req.put("jlxh", identity);


         /* //先判断物资库存 ,判断是否需要二级库房出库
         Map<String, Object> wl_xhmx = new HashMap<>();
            List<Map<String, Object>> wz_list = hosOrderBillMapper.wz1(req);
            req.put("csmc", "WZXHMX_WDZKF_HCJF");
            String WZXHMX_WDZKF_HCJF = hosOrderBillMapper.getXtcs(xtcsMap("WZXHMX_WDZKF_HCJF"));
            if (WZXHMX_WDZKF_HCJF == null || WZXHMX_WDZKF_HCJF.equals("")) {
                throw new CommonException("'机构未设置WZXHMX_WDZKF_HCJF对应数据来源的系统参数");
            }
            if (wz_list.size() > 0) {
                for (Map<String, Object> wz_map : wz_list) {
                    //查询科室物资库房对照
                    List<Map<String, Object>> ks_list = hosOrderBillMapper.wz6(wz_map);
                    if ("1".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        continue;
                    } else if ("0".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        throw new CommonException("ksdm" + wz_map.get("KSDM") + "未对应物资库房");
                    }
                    //查询物资库存是否满足
                    List<Map<String, Object>> wzkc_list = hosOrderBillMapper.wz2(wz_map);
                    if (wzkc_list.size() < 1) {
                        throw new CommonException("物资库存不足");
                    }
                    wz_map.putAll(wzkc_list.get(0));
                    //更新物资库存
                    count = hosOrderBillMapper.wz3(wz_map);
                    if (count != 1) {
                        throw new CommonException("物资库存不足,更新物资库存失败");
                    }
                    //写wl_xhmx
                    //10获取记录序号
                    Long jlxh = getIdentity("wl", "WL_XHMX");
                    wl_xhmx.put("jlxh", jlxh);
                    wl_xhmx.put("jgid", "1");
                    wl_xhmx.put("kfxh", wz_map.get("KFXH"));
                    wl_xhmx.put("ksdm", wz_map.get("KSDM"));
                    wl_xhmx.put("ksmc", wz_map.get("KSMC"));
                    wl_xhmx.put("brid", wz_map.get("BRID"));
                    wl_xhmx.put("brhm", wz_map.get("MZHM"));
                    wl_xhmx.put("brly", "2");
                    wl_xhmx.put("brxm", wz_map.get("BRXM"));
                    // wl_xhmx.put("xhrq",);
                    wl_xhmx.put("wzxh", wz_map.get("WZXH"));
                    wl_xhmx.put("wzsl", wz_map.get("YLSL"));
                    wl_xhmx.put("wzjg", wz_map.get("WZJG"));
                    wl_xhmx.put("thsl", 0);
                    wl_xhmx.put("ztbz", 0);
                    wl_xhmx.put("djxh", 0);
                    wl_xhmx.put("sbxh", wz_map.get("YJXH"));
                    wl_xhmx.put("kcxh", wz_map.get("KCXH"));
                    wl_xhmx.put("cjxh", wz_map.get("CJXH"));
                    wl_xhmx.put("yjxh", wz_map.get("SBXH"));
                    count = hosOrderBillMapper.wz5(wl_xhmx);
                }
            }*/
            req.put("qrgh", req.get("executionCode"));
            //3.zy_fymx写入
            count = hosOrderBillMapper.saveNoSQIDFymx(req);
            if (count == 0) throw new CommonException("zy_fymx写入失败" + req.get("jhh"));
            //2.bq_yzjh更新
            //2.1查询是否有医嘱计划
            List<Map<String, Object>> yzjhList = hosOrderBillMapper.getNoSQIDYZJH(req);
            if (CollectionUtils.isEmpty(yzjhList))
                throw new CommonException("未找到相关医嘱计划信息!!::" + req.get("ysyzbh"));

            count = hosOrderBillMapper.upNoSQIDYZJH(req);
            if (count == 0) {
                throw new CommonException("更新bq_zyjh失败!!::" + req.get("ysyzbh"));
            }
        }

        return thePlatformReturnedSuccessfully("HIS", "ODS_03070009_SUC", "3.3");

    }

    /*
     * 非检查医嘱 取消计费(住院 无申请点ID 如:药品,治疗等)
     */
    @Override
    @MultiDataSourceTransactional(transactionManagers = {"hisTransactionManager","portalTransactionManager"})
    public String noExamineOrderCancelBill(Map<String, Object> req) throws Exception {

        Map<String, Object> examStatusInfo = (Map<String, Object>) req.get("ExamStatusInfo");
        //判断入参不能为空
        isNUll(examStatusInfo, Arrays.asList("zyh", "ItemList"));

        //判断是否存在网络导致 二次调用接口
        List<Map<String, Object>> xmList = new ArrayList<>();
        if ((((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item")) instanceof List) {
            xmList = (List<Map<String, Object>>) ((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item");
        } else {
            xmList.add((Map<String, Object>) ((Map<String, Object>) examStatusInfo.get("ItemList")).get("Item"));
        }

        //获取住院号
        int n = 0;
        for (Map<String, Object> xm : xmList) {
            xm.put("zyh", examStatusInfo.get("zyh"));
            xm.put("jhh", xm.get("jhh"));
            xm.put("ysyzbh", xm.get("ysyzbh"));
            xm.put("ysdm", xm.get("executionCode"));
            xm.put("qrgh", xm.get("executionCode"));
            xm.put("hzrq", xm.get("executionTime"));
            xm.put("czgh", xm.get("executionCode"));

            //验证医生是否存在 验证操作工号
            List<Map<String, Object>> ysxxList = hosOrderBillMapper.getYsxx(xm);
            if (CollectionUtils.isEmpty(ysxxList))
                throw new CommonException("该医生代码医生[" + xm.get("executionCode") + "]不存在!");

            //判断是否需要二级库房出库
            Map<String, Object> wl_xhmx = new HashMap<>();
            List<Map<String, Object>> wz_list = hosOrderBillMapper.wz1(xm);
            req.put("csmc", "WZXHMX_WDZKF_HCJF");
            String WZXHMX_WDZKF_HCJF = hosOrderBillMapper.getXtcs(xtcsMap("WZXHMX_WDZKF_HCJF"));
            if (WZXHMX_WDZKF_HCJF == null || WZXHMX_WDZKF_HCJF.equals("")) {
                throw new CommonException("'机构未设置WZXHMX_WDZKF_HCJF对应数据来源的系统参数");
            }

            if (wz_list.size() > 0) {
                for (Map<String, Object> wz_map : wz_list) {
                    //查询科室物资库房对照
                    List<Map<String, Object>> ks_list = hosOrderBillMapper.wz6(wz_map);
                    if ("1".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        continue;
                    } else if ("0".equals(WZXHMX_WDZKF_HCJF) && ks_list.size() < 1) {
                        throw new CommonException("ksdm" + wz_map.get("KSDM") + "未对应物资库房");
                    }
                    //查询物资库存是否满足
                    List<Map<String, Object>> wzkc_list = hosOrderBillMapper.wz2Tf(wz_map);
                    if (CollectionUtils.isEmpty(wzkc_list))
                        throw new CommonException("WZXH" + wz_map.get("WZXH") + "预扣数量小于退费数量，更新物资库存失败");
                    wz_map.putAll(wzkc_list.get(0));
                    n = hosOrderBillMapper.wz3Tf(wz_map);
                    if (n != 1)
                        throw new CommonException("WZXH" + wz_map.get("WZXH") + "预扣数量小于退费数量,更新物资库存失败");
                    //写wl_xhmx,获取记录序号
                    Long jlxh = getIdentity("wl", "WL_XHMX");
                    wl_xhmx.put("jlxh", jlxh);
                    wl_xhmx.put("jgid", "1");
                    wl_xhmx.put("kfxh", wz_map.get("KFXH"));
                    wl_xhmx.put("ksdm", wz_map.get("KSDM"));
                    wl_xhmx.put("ksmc", wz_map.get("KSMC"));
                    wl_xhmx.put("brid", wz_map.get("BRID"));
                    wl_xhmx.put("brhm", wz_map.get("MZHM"));
                    wl_xhmx.put("brly", "2");
                    wl_xhmx.put("brxm", wz_map.get("BRXM"));
                    // wl_xhmx.put("xhrq",);
                    wl_xhmx.put("wzxh", wz_map.get("WZXH"));
                    wl_xhmx.put("wzsl", wz_map.get("TFSL"));
                    wl_xhmx.put("wzjg", wz_map.get("WZJG"));
                    wl_xhmx.put("thsl", 0);
                    wl_xhmx.put("ztbz", 0);
                    wl_xhmx.put("djxh", 0);
                    wl_xhmx.put("sbxh", wz_map.get("fymxJLXH"));
                    wl_xhmx.put("kcxh", wz_map.get("KCXH"));
                    wl_xhmx.put("cjxh", wz_map.get("CJXH"));
                    wl_xhmx.put("yjxh", wz_map.get("SBXH"));
                    n = hosOrderBillMapper.wz5(wl_xhmx);
                }
            }
            //更新 BQ_YZJH
            n = hosOrderBillMapper.updateNOSQIDBQYZJH(xm);
            if (n < 1 || n > 500)throw new CommonException("根据检查项目代码OrderId查到有效的医嘱计划，是否医嘱计划未执行");

            xm.put("jlxh", getIdentity("zy", "ZY_FYMX", n));
            logger.info("jlxh的值：" + getIdentity("zy", "ZY_FYMX", n));
            //更新zy_fymx YTSL
            n = hosOrderBillMapper.updateFYMXYTSL(xm);
            if (n < 1 || n > 500) throw new CommonException("更新zy_fymx.YTSL失败");
            //查询zy_fymx
            List<Map<String, Object>> fymxList = hosOrderBillMapper.getFYMXZF(xm);
            for (Map<String, Object> fymx : fymxList) {
                n = hosOrderBillMapper.saveFYMXZF(fymx);
                if (n == 0) throw new CommonException("zy_fymx负记录写入失败!!!");
            }
/*            //插入数据BQ_TFMX
            //更新 BQ_TFMX
            List<Map<String, Object>> tfmxList = hosOrderBillMapper.getBQTFMX(xm);
            if (tfmxList.size()<1){
                throw new CommonException("病区退费明细无记录，未退费。");
            }
            for (Map<String,Object> bqtfmx:tfmxList) {
                bqtfmx.put("TFRQ",xm.get("refundTime"));
                bqtfmx.put("SHRQ",xm.get("refundTime"));
                bqtfmx.put("czgh",xm.get("executionCode"));
                //更新 BQ_TFMX
                n =   hosOrderBillMapper.saveBQTFMX(bqtfmx);
                if (n == 0){
                    throw new CommonException("BQ_TFMX表更新状态失败");
                }
            }
            //更新YJ_ZY01
            n = hosOrderBillMapper.updateZY01Zf(xm);
            if (n < 1 || n > 500) {
                throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY01,是否医嘱未执行");
            }
            //更新YJ_ZY02
            n = hosOrderBillMapper.updateZY02Zf(xm);
            if (n < 1 || n > 500) {
                throw new CommonException("根据检查项目代码OrderId查到有效的YJ_ZY02,是否医嘱未执行");
            }
            req.put("zlxmid", zlxmid);
            req.put("JCZT", "1");
            req.put("OrderId", xm.get("OrderId"));
            n = hosOrderBillMapper.updateEMR_JCXM(req);
            if (n < 1 || n > 500) {
                throw new CommonException("根据申请单号" + sqdh + ",未找到子项目" + zlxmid);
            }

            Map<String, Object> jzxh = hosOrderBillMapper.getJZXH(xm);

            List<Map<String, Object>> bqyzList = hosOrderBillMapper.getQXBQYZ(jzxh);
            for (Map<String, Object> map : bqyzList) {
                Long jlxh = getIdentity("yj", "YJ_BG01_JJHS");
                map.put("JLXH", jlxh);
                map.put("JCYS", xm.get("ysdm"));
                map.put("MZZY", "2");
                map.put("JCRQ", xm.get("executionTime"));
                try {
                    int a = hosOrderBillMapper.insertJJHS(map);
                    if (a == 0) {
                        logger.info("取消经济核算表插入失败");
                    }
                } catch (Exception e) {
                    logger.info("取消经济核算表插入失败");
                }
            }*/
        }
        return thePlatformReturnedSuccessfully("HIS", "ODS_03070009_SUC", "3.3");
    }

    /*
     * 门诊经济核算报告
     */
    public void outpatientAccountingReport(List<Map<String, Object>> bqyzList, Map<String, Object> xm, String ExamStatusCode, String czgh, Map<String, Object> req) {

        Map<String, Object> jzxh = new HashMap<>();
        jzxh.put("YJXH", xm.get("OrderId"));
        if ("6".equals(ExamStatusCode)) {
            jzxh.put("zxys", "");
            jzxh.put("zxpb", "0");
            jzxh.put("zxrq", "");
            jzxh.put("sqid", xm.get("sqdh"));
            int a = hosOrderBillMapper.updateMSYJ01(jzxh);
            if (a == 0) {
                throw new CommonException("更新ms_yj01失败::医技序号是==" + jzxh.get("YJXH"));
            }
            bqyzList = hosOrderBillMapper.getQXMZYZ(jzxh);
        } else if ("5".equals(ExamStatusCode)) {
            jzxh.put("zxys", czgh);
            jzxh.put("zxpb", "1");
            jzxh.put("zxrq", new Date());
            jzxh.put("sqid", xm.get("sqdh"));
            if ("5".equals(ExamStatusCode) || "7".equals(ExamStatusCode)) {
                int a = hosOrderBillMapper.updateMSYJ01(jzxh);
                if (a == 0) {
                    throw new CommonException("更新ms_yj01失败::医技序号是==" + jzxh.get("YJXH"));
                }
            }
            bqyzList = hosOrderBillMapper.getMZYZ(jzxh);
        }

        if (bqyzList != null) {
            for (Map<String, Object> map : bqyzList) {
                Long jlxh = getIdentity("yj", "YJ_BG01_JJHS");
                map.put("JLXH", jlxh);
                map.put("JCYS", czgh);
                map.put("MZZY", "1");
                map.put("JCRQ", ((Map<String, Object>) req.get("ExamStatusInfo")).get("OperatingDateTime"));
                try {
                    int c = hosOrderBillMapper.insertJJHS(map);
                    if (c == 0) logger.info("经济核算表插入失败");
                } catch (Exception e) {
                    logger.info("经济核算表插入失败" + e.getMessage());
                }
            }
        }

    }
}
